package youcaihuaoa.weetop.com.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.util.LiveDataBus
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.BR
import youcaihuaoa.weetop.com.R
import youcaihuaoa.weetop.com.databinding.AppFragmentShopBinding
import youcaihuaoa.weetop.com.ui.adapter.ShopItemAdapter
import youcaihuaoa.weetop.com.vm.main.ShopFragmentViewModel

class ShopFragment : GLBaseFragment<AppFragmentShopBinding, ShopFragmentViewModel>() {

    private lateinit var shopItemAdapter: ShopItemAdapter


    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_shop

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        shopItemAdapter = ShopItemAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                if (shopItemAdapter.data[position].template_type == "99") {
                    ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_TREND_APPLY_PROFILE,
                        Bundle().apply
                        {
                            putString(AppConstants.BundleKey.ZDY_FORM_ID, shopItemAdapter.data[position].zdy_form_id)
                            putString(AppConstants.BundleKey.ID, shopItemAdapter.data[position].id)
                            putString(AppConstants.BundleKey.TITLE, shopItemAdapter.data[position].public_title)
                            putString(AppConstants.BundleKey.TYPE, shopItemAdapter.data[position].template_type)
                            putInt(AppConstants.BundleKey.STATUS, 2)
                        })
                } else {
                    ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_MY_APPLY_PROFILE,
                        Bundle().apply
                        {
                            putString(AppConstants.BundleKey.ID, shopItemAdapter.data[position].id)
                            putString(AppConstants.BundleKey.TITLE, shopItemAdapter.data[position].public_title)
                            putString(
                                AppConstants.BundleKey.TYPE,
                                shopItemAdapter.data[position].template_type
                            )
                            putInt(AppConstants.BundleKey.STATUS, 2)
                        })
                }
            }
        }
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = shopItemAdapter
        }

        viewModel.examineUserData()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uiChangeEvent.dataEvent.observe(this) {
            it ?: return@observe
            shopItemAdapter.setList(it)
        }

        LiveDataBus.get().with(AppConstants.LiveKey.REFRESH_APPLY).observe(this){
            viewModel.examineUserData()
        }
    }
}